﻿using System;
using System.Web.Mvc;
using Dentiste.Domain;
using Dentiste.Domain.Factories;
using Dentiste.Domain.Interfaces;
using Dentiste.UI.Models;

namespace Dentiste.UI.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index(string firstname, string lastname)
        {
            IInvoice i;
            if (string.IsNullOrEmpty(RouteData.Values["id"] as string))
            {
                i = new Factory<IInvoice>().Create();
                if (!string.IsNullOrEmpty(lastname) && !string.IsNullOrEmpty(firstname))
                {
                    i.Patient = DentisteManager.Instance.SelectPatient(firstname, lastname);
                    if (i.Patient == null)
                    {
                        IPatient p = new Factory<IPatient>().Create();
                        p.FirstName = firstname;
                        p.LastName = lastname;
                        DentisteManager.Instance.InsertPatient(p);
                        i.Patient = p;
                    }
                }
                i.Date = DateTime.Now;
                i = DentisteManager.Instance.InsertInvoice(i);
            }
            else
            {
                i = DentisteManager.Instance.SelectInvoice(RouteData.Values["id"] as String);
                if (i == null)
                {
                    throw new Exception("No invoice binded with that instance of the application");
                }
            }

            var viewModel = new IndexViewModel
            {
                FirstName = (i.Patient != null ? i.Patient.FirstName : string.Empty),
                LastName = (i.Patient != null ? i.Patient.LastName : string.Empty),
                Invoice = i
            };

            return View(viewModel);
        }

        [HttpPost]
        public void AddRow(string dateStr, string inamiCode, int toothId, int invoiceId, string firstname,
            string lastname)
        {
            IInvoice i = DentisteManager.Instance.SelectInvoice(invoiceId);
            if (i == null)
            {
                throw new Exception("No invoice binded with that instance of the application");
            }
            i.Patient = DentisteManager.Instance.SelectPatient(firstname, lastname);
            if (i.Patient == null ||
                (string.IsNullOrEmpty(i.Patient.LastName) && string.IsNullOrEmpty(i.Patient.LastName)))
            {
                IPatient p = new Factory<IPatient>().Create();
                p.FirstName = firstname ?? string.Empty;
                p.LastName = lastname ?? string.Empty;
                DentisteManager.Instance.InsertPatient(p);
                i.Patient = p;
                DentisteManager.Instance.UpdateInvoice(i);
            }

            IRow r = new Factory<IRow>().Create();
            r.Inami = DentisteManager.Instance.SelectInami(inamiCode);
            r.ToothNumber = toothId;
            r.Date = DateTime.Parse(dateStr);
            r = DentisteManager.Instance.InsertRow(r);
            i.Rows.Add(r);

            foreach (IInami inami in r.Inami.RequiredInamis)
            {
                IRow r2 = new Factory<IRow>().Create();
                r2.Inami = DentisteManager.Instance.SelectInami(inami.Code);
                r2.ToothNumber = toothId;
                r2.Date = r.Date;
                r2 = DentisteManager.Instance.InsertRow(r2);
                i.Rows.Add(r2);
            }
            DentisteManager.Instance.UpdateInvoice(i);
        }

        [HttpPost]
        public void RemoveRow(int rowId)
        {
            IRow row = DentisteManager.Instance.SelectRow(rowId);
            DentisteManager.Instance.DeleteRow(row);
        }

        [HttpPost]
        public void UpdatePatient(string firstName, string lastName, int invoiceId)
        {
            if (string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName))
            {
                return;
            }
            IInvoice invoice = DentisteManager.Instance.SelectInvoice(invoiceId);
            IPatient patient = DentisteManager.Instance.SelectPatient(firstName, lastName);
            if (patient == null)
            {
                patient = new Factory<IPatient>().Create();
                patient.LastName = lastName;
                patient.FirstName = firstName;
                DentisteManager.Instance.InsertPatient(patient);
            }
            invoice.Patient = patient;
            DentisteManager.Instance.UpdateInvoice(invoice);
        }
    }
}